package com.bfhd.account.ui;

import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bfhd.account.R;
import com.bfhd.account.databinding.AccountActivityModifyPwdBinding;
import com.bfhd.account.vm.AccountViewModel;
import com.bfhd.circle.base.HivsBaseActivity;
import com.dcbfhd.utilcode.utils.EncryptUtils;
import com.dcbfhd.utilcode.utils.ToastUtils;
import com.docker.common.common.router.AppRouter;
import com.docker.common.common.utils.cache.CacheUtils;

import java.util.HashMap;

import javax.inject.Inject;

/*
 * 修改密码
 **/
@Route(path=AppRouter.ACCOUNT_MODIFY_PWD)
public class AccounModifyPwdActivity extends HivsBaseActivity<AccountViewModel, AccountActivityModifyPwdBinding> {


    @Inject
    ViewModelProvider.Factory factory;


    @Override
    public AccountViewModel getmViewModel() {
        return ViewModelProviders.of(this, factory).get(AccountViewModel.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.account_activity_modify_pwd;
    }

    @Override
    protected void inject() {
        super.inject();
        ARouter.getInstance().inject(this);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mViewModel.resetpwd.observe(this, s -> {
           if(!TextUtils.isEmpty(s)){
              ToastUtils.showShort("修改成功");
              finish();
           }else {
               ToastUtils.showShort("修改失败，请重试");
           }
        });

    }

    @Override
    public void initView() {
        mToolbar.setTitle("修改密码");

       mBinding.btnSure.setOnClickListener(v -> {
           if(TextUtils.isEmpty(mBinding.edOldPwd.getText().toString())){
               ToastUtils.showShort("请输入旧密码");
               return;
           }
           if(TextUtils.isEmpty(mBinding.edNewPwd.getText().toString())){
               ToastUtils.showShort("请输入新密码");
               return;
           }
           if(TextUtils.isEmpty(mBinding.edSurePwd.getText().toString())){
               ToastUtils.showShort("请输入确认密码");
               return;
           }

           if(!mBinding.edNewPwd.getText().toString().equals(mBinding.edSurePwd.getText().toString())){
               ToastUtils.showShort("两次输入的密码不一致");
               return;
           }

           HashMap<String,String> param = new HashMap<>();
           param.put("uuid", CacheUtils.getUser().uuid);
           param.put("oldpsw", EncryptUtils.encryptMD5ToString(mBinding.edOldPwd.getText().toString()));
           param.put("newpsw", EncryptUtils.encryptMD5ToString(mBinding.edNewPwd.getText().toString()));
           mViewModel.resetpwdfun(param);

           //updatePassword

       });
    }

}
